/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.password.encoder;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;

/**
 * 密码加密组件
 *
 * @author 挺好的 2023年06月05日 9:15
 */
@Component ("passwordEncoder")
public class PasswordEncoder {

    /**
     * 盐值
     */
    @Value ("${password-salt}")
    private String passwordSalt;

    /**
     * 获取密码加密器
     *
     * @return {@link BCryptPasswordEncoder}
     */
    public BCryptPasswordEncoder getPasswordEncoder () {
        SecureRandom secureRandom = new SecureRandom(this.passwordSalt.getBytes(StandardCharsets.UTF_8));
        return new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2B, secureRandom);
    }
}
